"use strict";
const nodemailer = require("nodemailer");

/**
 * 
 * @param {*} toUser user email address
 * @param {*} title email title
 * @param {*} message email info
 * @param {*} html <html></html>
 */
// async..await is not allowed in global scope, must use a wrapper
async function main(toUser,title,message,html){

  // Generate test SMTP service account from ethereal.email
  // Only needed if you don't have a real mail account for testing
  let account = await nodemailer.createTestAccount();

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: "smtp.163.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: account.user, // generated ethereal user
      pass: account.pass // generated ethereal password
    }
  });

  // setup email data with unicode symbols
  let mailOptions = {
    from: '"xxx" <xxxxxx@163.com>', // sender address
    to: toUser, // list of receivers
    subject: title, // Subject line
    text: message, // plain text body
    html: html // html body
  };

  // send mail with defined transport object
  let info = await transporter.sendMail(mailOptions)

  console.log("Message sent: %s", info.messageId);
  // Preview only available when sending through an Ethereal account
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));

  // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
  // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
  return true
}

// main('订单编号5555').catch(console.error);
module.exports = main